package com.project.controller;

import com.project.model.entity.ReportAnnotation;
import com.project.result.Result;
import com.project.service.ReportAnnotationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/reportAnnotation")
public class ReportAnnotationController {
    private final ReportAnnotationService reportAnnotationService;

    @Autowired
    public ReportAnnotationController(ReportAnnotationService reportAnnotationService) {
        this.reportAnnotationService = reportAnnotationService;
    }

    @PostMapping("/saveOrUpdate")
    public Result<ReportAnnotation> saveOrUpdateReportAnnotation(@RequestBody ReportAnnotation reportAnnotation) {
        try {
            if (reportAnnotation == null) {
                return Result.fail("注释内容为空");
            }
            System.out.println(reportAnnotation.toString());

            if (reportAnnotationService.getReportAnnotationById(reportAnnotation.getId()) == null) {
                reportAnnotation.setStatus("reviewing");
                ReportAnnotation savedAnnotation = reportAnnotationService.addReportAnnotation(reportAnnotation);
                return savedAnnotation != null ? Result.ok(savedAnnotation) : Result.fail("保存失败");
            } else {
                ReportAnnotation updatedAnnotation = reportAnnotationService.updateReportAnnotation(reportAnnotation);
                return updatedAnnotation != null ? Result.ok(updatedAnnotation) : Result.fail("更新失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("系统异常");
        }
    }


    @DeleteMapping("/delete")
    public Result<Void> deleteReportAnnotation(@RequestParam Integer id) {
        try {
            if (id == null || id <= 0) {
                return Result.fail("ID值无效");
            }
            reportAnnotationService.deleteReportAnnotation(id);
            return Result.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("系统异常");
        }
    }

    @GetMapping("/getById")
    public Result<ReportAnnotation> getReportAnnotationById(@RequestParam Integer id) {
        try {
            if (id == null || id <= 0) {
                return Result.fail("ID值无效");
            }
            ReportAnnotation reportAnnotation = reportAnnotationService.getReportAnnotationById(id);
            return reportAnnotation != null ? Result.ok(reportAnnotation) : Result.fail("未找到该报告注解");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("系统异常");
        }
    }

    @GetMapping("/getAll")
    public Result<List<ReportAnnotation>> getAllReportAnnotations() {
        try {
            List<ReportAnnotation> reportAnnotations = reportAnnotationService.getAllReportAnnotations();
            return !reportAnnotations.isEmpty() ? Result.ok(reportAnnotations) : Result.fail("没有数据");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("系统异常");
        }
    }

    //由报告ID获取所有状态的报告注解
    @GetMapping("/getByReportId")
    public Result<List<ReportAnnotation>> getReportAnnotationsByReportId(@RequestParam Integer reportId) {
        try {
            if (reportId == null || reportId <= 0) {
                return Result.fail("报告ID无效");
            }
            List<ReportAnnotation> reportAnnotations = reportAnnotationService.getReportAnnotationsByReportId(reportId);
            return !reportAnnotations.isEmpty() ? Result.ok(reportAnnotations) : Result.fail("未找到报告注解");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("系统异常");
        }
    }

    //根据报告ID获取当前状态为reviewing的报告注解
    @GetMapping("/getReviewingReportByReportId")
    public Result<ReportAnnotation> getReviewingReportByReportId(@RequestParam Integer reportId) {
        try {
            if (reportId == null || reportId <= 0) {
                return Result.fail("报告ID无效");
            }
            ReportAnnotation reportAnnotation = reportAnnotationService.getReviewingAnnotationByReportId(reportId);
            return reportAnnotation!=null ? Result.ok(reportAnnotation) : Result.fail("未找到报告注解");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("系统异常");
        }
    }
}

